package Bluetooth_NXT;

//import java.util.zip.Adler32;
//import java.util.zip.Checksum;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.LCD;
public class NXTStack
{
	private long _checkSum;
//	private Checksum _adler32;
	private int _arraySize; //size of sensor data array
	private int[] _sensorData = new int[_arraySize];//stores the sensor values
	private static int _lv; 
	private static NXTStack _instance = null;	
	private BTCommunication_Client _btClient;
	private String _btServerName;
	
	private NXTStack()
	{
//		_adler32 = new Adler32();
		_checkSum = 123456789; //TODO: make it right
		_arraySize = 256; 
		_sensorData = new int[_arraySize];
		_lv = 0;
		if (_btServerName != null && _btServerName != "")
			_btClient = new BTCommunication_Client(_btServerName);
		else
			_btClient = new BTCommunication_Client("nb-daniel");
		_btClient.ConnectServer();
	}
	public static NXTStack GetInstance()
	{
		if (_instance == null)
			_instance = new NXTStack();
		return _instance;
	}
//	public void SetBtServerName(String BtServerName)
//	{
//		_btServerName = BtServerName;
//	}
//	public void EndTransmission()
//	{
//		if(_instance != null)
//		{
//			try
//			{
//				_btClient.FinishTalk();
//				_btClient.DisconnectServer();		
//			}catch (Exception e){
//				LCD.drawString("Error\nin End\ntransmission", 0, 0);
//			}
//		}
//	}
//	public void AddSensorValue(float value)
//	{
//		if(_lv < _arraySize)
//			_sensorData[_lv++] = (int)value;
//		else
//			StartTransmit();//if Stack is full
//	}
//	public void StartTransmit()
//	{	
////		byte[] toSend = IntToByte(_sensorData);
//		byte[] toSend = new byte[]{1,2,3,4};
//		LCD.drawString("Sending", 0, 0);
//		LCD.drawString(toSend[0] + " " + toSend[1] + "\n" + toSend[2] + " " + toSend[3], 0, 1);
//		try{
//			Thread.sleep(3000);
//			}
//			catch (InterruptedException e){}
//			LCD.clear();
//		DataOutputStream oStream = _btClient.GetNXTConnection().openDataOutputStream();
//		try
//		{
//			oStream.write(toSend, 0, toSend.length);
//			oStream.flush();
//			oStream.close();
//		}catch (IOException e){}	
//	}
	public byte[] IntToByte(int[] iArray)
	{
		int offset = 128;
		int value;
		byte[] bArray = new byte[_lv];//only valid values
		for (int i = 0; i < _lv; i++)
		{
			value = iArray[i] - offset; //calculate byte => -128 < b < 127
			bArray[i] = (byte)value;
		}
		return bArray;
	}
	private int[] byteToInt(byte[] bArray)
	{
		int offset = 128;
		int value;
		int[] iArray = new int[bArray.length];
		for (int i = 0; i < bArray.length; i++)
		{
			value = (int)bArray[i]; //calculate int
			iArray[i] = value + offset;
		}
		return iArray;
	}
	private void SetAndCalculateCheckSum(byte[] bArray)
	{
//		_adler32.update(bArray, 0, bArray.length);
	}
	public float CalculateChecksum(int[] toCheck)
	{
		int groesse = toCheck.length;
		float summe = 0;
		for (int i = 0;i< groesse; i++)
			summe += toCheck[i]; 
		return summe/groesse;
		
	}
	private long GetCheckSum()
	{
//		_checkSum = _adler32.getValue();
		return _checkSum;
	}
}

